package ssov5

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/micro-plat/hydra"
	"github.com/micro-plat/lib4go/errs"
)

func remoteRequest(host, path, content string, data interface{}) (interface{}, http.Header, error) {

	listByte, header, err := remoteRequestString(host, path, content)
	if err != nil {
		return nil, nil, err
	}
	err = json.Unmarshal(listByte, data)
	if err != nil {
		return nil, nil, fmt.Errorf("字符串转json发生错误，err：%v;org:%s;%s%s", err, string(listByte), host, path)
	}

	return data, header, nil
}
func remoteRequestString(host, path, content string) ([]byte, http.Header, error) {
	url := host + path
	client := hydra.C.HTTP().GetRegularClient()
	body, header, statusCode, err := client.HRequest("POST", url, content, "utf-8", nil)
	if err != nil {
		return nil, nil, err
	}
	if statusCode != 200 {
		return nil, nil, errs.NewErrorf(statusCode, "获取apiserver信息失败, url: %s,HttpStatus:%d, body:%s", url, statusCode, body)
	}
	return body, header, nil
}
